package me.shurufa.holder;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.model.Book;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 搜索图书列表itemViewHolder
 *
 * @FileName: me.shurufa.holder.SearchBookViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-28 11:52
 */
public class SearchBookViewHolder extends BaseViewHolder{

    @Bind(R.id.book_img)
    ImageView bookImg;
    @Bind(R.id.book_title)
    TextView bookTitle;
    @Bind(R.id.book_author)
    TextView bookAuthor;
    @Bind(R.id.book_factory)
    TextView bookFactory;
    @Bind(R.id.item_root)
    LinearLayout itemRoot;

    private Context mContext;
    private Book mBook;

    public SearchBookViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        addOnClickListener(bookImg);
        addOnClickListener(itemRoot);
    }

    public void bind(Book book){
        if (book==null){
            return;
        }
        mBook = book;
        if (!TextUtils.isEmpty(book.image)){
            Glide.with(mContext).load(book.image).into(bookImg);
        }else {
            //TODO 设置默认图片
        }

        if (!TextUtils.isEmpty(book.title)){
            bookTitle.setText(book.title);
        }else {
            bookTitle.setText("未知");
        }

        if (!TextUtils.isEmpty(book.author)){
            bookAuthor.setText("作者: "+book.author);
        }else {
            bookAuthor.setText("作者: 未知" );
        }

        if (!TextUtils.isEmpty(book.publisher)){
            bookFactory.setText("出版社: "+book.publisher);
        }else {
            bookFactory.setText("出版社: 未知");
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.item_root:
            case R.id.book_img:
                EventObject object = new EventObject();
                object.setEventAction(Constants.EVENT_STAR_BOOK);
                object.setBook(mBook);
                EventBus.getDefault().post(object);
                break;
        }
    }
}
